<?php

class News_model extends CI_Model {
	
	protected $table;

	function __construct(){
		parent::__construct();
		$this->table = 'news';
	}

	
	
	function insert($title , $content){
		$this->db->insert($this->table, array('title'=>$title, 'content'=>$content, 'add_date'=>date('Y-m-d'), 'modif_date'=>date('Y-m-d')));
	}

	function get_news($number = 0, $limit = 0){
		if($number == 0 && $limit == 0){
			$res = 	$this->db->order_by('add_date', 'asc')
					->get($this->table)
					->result_array();
		}
		else
			$res = 	$this->db->order_by('add_date', 'asc')
					->get($this->table)
					->result_array($number, $limit);
		return $res;	
	}

	function convert_date($date){
		$split = explode("-",$date);
		return $split[2].'-'.$split[1].'-'.$split[0];
	}

	function get_news_by_id($id){
		$res = 	$this->db->get_where($this->table, array('id_news'=>$id))
				->result_array();
		return $res[0];
	}

	function update_news($id, $title, $content){
		$this->db->where('id_news',$id)->update($this->table, array('title'=>$title, 'content'=>$content));
	}

	function remove_news($id){
		$this->db->delete($this->table, array('id_news'=>$id));
	}

}
?>